// pages/menu.js
const app = getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    current: 0,
    categoryGoodsList: [],
    cartList: [], //购物车，
    showCartModal: true, // 是否显示购物车,
    showGoodInfoDialog: false, //是否显示 商品详情模态框
    selectGoods: {}
  },
  openGoodInfo(e) {
    const good = e.currentTarget.dataset.good;
    this.setData({
      showGoodInfoDialog: true,
      selectGoods: good
    })
    console.log("openGoodInfo=" + good)
  },
  mutlHandleAddToCart(e) { //多规格添加到购物车
    console.log(e.detail)
    const goods = e.detail.data.goods;
    const propsText = e.detail.data.propsText;
    const mutlPrice = e.detail.data.mutlPrice;
    const props = e.detail.data.props;
    const goodsCategoryId = goods.goodsCategoryId;
    let number = parseInt(e.detail.data.number);
    let cartList = this.data.cartList
    console.log("data cartList=" + cartList.length)
    const index = cartList.findIndex(item => {
      return (item.spuId === goods.spuId) && (item.propsText === propsText)
    })
    console.log("good.specType=index=" + index)
    if (index > -1) {
      cartList[index].number += 1;
    } else {
      let cartGood = {
        spuId: goods.spuId,
        goodsCategoryId: goodsCategoryId,
        spuTitle: goods.spuTitle,
        spuImage:goods.spuImage,
        spuSubTitle: goods.spuSubTitle,
        spuSellPrice: mutlPrice,
        number: number,
        specType: goods.specType,
        propsText: propsText,
        props: props
      }
      cartList.push(cartGood);
    }
    this.setData({
      cartList: cartList,
      showGoodInfoDialog: false
    })
    if (cartList.length > 0) {
      this.setData({
        showCartModal: false
      })
    }
    app.setCartList(cartList)
  },
  handleAddToCart(e) { //添加到购物车
    const goods = e.currentTarget.dataset.goods;
    const goodsCategoryId = goods.goodsCategoryId;
    let number = parseInt(e.currentTarget.dataset.number);
    let cartList = this.data.cartList
    const index = cartList.findIndex(item => {
      if (goods.specType) {
        return (item.spuId === goods.spuId) && (item.propsText === goods.propsText)
      } else {
        return item.spuId === goods.spuId
      }
    })
    console.log("good.specType=index=" + index)
    if (index > -1) {
      cartList[index].number += 1;
    } else {
      let cartGood = {
        spuId: goods.spuId,
        goodsCategoryId: goodsCategoryId,
        spuTitle: goods.spuTitle,
        spuSubTitle: goods.spuSubTitle,
        spuSellPrice: goods.spuSellPrice,
        spuImage:goods.spuImage,
        number: number,
        specType: goods.specType
      }
      cartList.push(cartGood);
    }
    this.setData({
      cartList: cartList,
      showGoodInfoDialog: false
    })
    if (cartList.length > 0) {
      this.setData({
        showCartModal: false
      })
    }
    app.setCartList(cartList)
    // spuId: good.spuId,
    //     goodsCategoryId: category.goodsCategoryId,
    //     name: good.naspuSubTitleme,
    //     price: good.spuSellPrice,
    //     number: number,
    //     image: good.images,
    //     specType: good.specType,
    //     props_text: good.props_text,
    //     props: good.props
  },
  getCategoryGoodsList: function () {
    app.wxRequest('GET', '/goodsCategory/getCategoryGoodsList', null, (res) => {
      if (200 == res.code) {
        this.setData({
          categoryGoodsList: res.data
        })
      }
    }, (err) => {
      console.log("登录返回11")
      console.log(err.errMsg)
    })
  },
  setCurrent(e) {
    let categoryId = e.currentTarget.dataset.current
    this.setData({
      current: categoryId
    })
  },
  clearCart() {
    this.setData({
      cartList: []
    })
    app.clearCart()
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.getCategoryGoodsList();
    console.log("onLoaddsds" + app.globalData.cartList)
    let cartList = app.globalData.cartList
    if (cartList.length > 0) {
      this.setData({
        showCartModal: false
      })
    }
    console.log("onLoad" + cartList.length)
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
    const cartList = wx.getStorageSync('cartList')
    console.log("onReady=", cartList)

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    const cartList = wx.getStorageSync('cartList')
    if (cartList) {
      this.setData({
        cartList: cartList
      })
    }
    console.log("onShow", this.data.cartList)
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})